package com.lb.jwitter.hibernate;

import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.lb.utils.orm.hibernate.HibernateGenericDao;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.Tbl_QLDTTXA_DM_ChiNhanh;

@Repository("tbl_QLDTTXA_DM_ChiNhanhDAO")
public class Tbl_QLDTTXA_DM_ChiNhanhHibernateDAO extends HibernateGenericDao<Tbl_QLDTTXA_DM_ChiNhanh, Long> implements Tbl_QLDTTXA_DM_ChiNhanhDAO {
   private Log log = LogFactory.getLog(Tbl_QLDTTXA_DM_ChiNhanhHibernateDAO.class);

   @Autowired
   public Tbl_QLDTTXA_DM_ChiNhanhHibernateDAO(@Qualifier("sessionFactory")   SessionFactory sessionFactory) {
       super(sessionFactory);
   }

   @SuppressWarnings("unchecked")
@Transactional
   public final List<Tbl_QLDTTXA_DM_ChiNhanh> findAll(int pageSize, int page) {
       if( pageSize == 0) return getSession().createCriteria(Tbl_QLDTTXA_DM_ChiNhanh.class).list(); 
       return getSession().createCriteria(Tbl_QLDTTXA_DM_ChiNhanh.class).setFirstResult(pageSize * (page-1)).setMaxResults(pageSize).list();
   }
   
@Transactional
   public final Long save(Tbl_QLDTTXA_DM_ChiNhanh _newInstance) {
   	return super.save(_newInstance);
   }

//@Transactional
//   public final boolean exists(Long id) {
//       boolean result = (Integer) getSession().createCriteria(Tbl_QLDTTXA_DM_ChiNhanh.class).setProjection(Projections.rowCount()).add(
//                       Restrictions.eq("id", id)).uniqueResult() == 1;
//       log.info("Result: "+result);
//   	return result;
//   }

@Transactional
   public Collection<Tbl_QLDTTXA_DM_ChiNhanh> find(Tbl_QLDTTXA_DM_ChiNhanh obj,int pageSize,int page){
   	Collection<Tbl_QLDTTXA_DM_ChiNhanh> _tbl_QLDTTXA_DM_ChiNhanhs = new ArrayList<Tbl_QLDTTXA_DM_ChiNhanh>();
   	try {
   		String sql = "select d from tbl_QLDTTXA_DM_ChiNhanh as d where 1=1 ";
   sql += " and d.id = " + obj.getId() + " ";
   sql += " and d.Email = " + obj.getEmail() + " ";
   sql += " and d.Ten = " + obj.getTen() + " ";
   sql += " and d.DiaChi = " + obj.getDiaChi() + " ";
   sql += " and d.DienThoai = " + obj.getDienThoai() + " ";
   sql += " and d.Fax = " + obj.getFax() + " ";
   		sql += " order by d.id DESC ";
   		_tbl_QLDTTXA_DM_ChiNhanhs = getSession().createQuery(sql).setFirstResult(pageSize*(page-1)).setMaxResults(pageSize).list();
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return _tbl_QLDTTXA_DM_ChiNhanhs;
   }

}//endclass
